#!/usr/local/bin/python
# -*- coding:utf-8 -*-
# Author: liuchong
# Time: 20-08-20 下午3:30
# Desc: 短信http接口的python代码调用示例
import http.client
import urllib
import json
from config.setting import host, account, password, port, balance_get_uri, sms_send_uri


def get_user_balance():
    """
    取账户余额
    """
    params = {'account': account, 'password': password}
    params = json.dumps(params)

    headers = {"Content-type": "application/json"}
    conn = http.client.HTTPConnection(host, port=port)
    conn.request('POST', balance_get_uri, params, headers)
    response = conn.getresponse()
    response_str = response.read()
    conn.close()
    return response_str


def send_sms(text, phone):
    """
    能用接口发短信
    """

    params = {'account': account, 'password': password, 'msg': '您的动态验证码为{}请在页面输入完成验证。如非本人操作请忽略。'.format(text), 'phone': phone, 'report': 'false'}
    params = json.dumps(params)

    headers = {"Content-type": "application/json"}
    conn = http.client.HTTPConnection(host, port=port, timeout=30)

    conn.request("POST", sms_send_uri, params, headers)
    response = conn.getresponse()
    response_str = response.read()
    conn.close()
    return response_str



# if __name__ == '__main__':
#     phone = "13545233790"
#     # 设置您要发送的内容：其中“【】”中括号为运营商签名符号，多签名内容前置添加提交
#     text = "【乐活商城】您的验证码是1234"
#
#     # 查账户余额
#     # print(get_user_balance())
#
#     # 调用智能匹配模版接口发短信
#     print(send_sms(text, phone))
